<?php
// TbBox
$this->beginWidget('bootstrap.widgets.TbBox', array(
	'title' => array(
		$this->projectName => array('project/schema'),
		'数据列表' => array('data/list'),
		'导入数据',
	),
	'headerIcon' => 'icon-plus-sign',
));

// Form
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id' => 'form-import',
	'type' => 'horizontal',
	'focus' => array($model, 'dbname'),
	'enableAjaxValidation' => true,
	'enableClientValidation' => true,
	'clientOptions' => array(
		'validateOnSubmit' => true,
		'beforeValidate' => 'js:function(form) { $(\'#' . CHtml::ActiveId($model, 'offset') . '\').val(-1); return true; }',
		'afterValidate' => 'js:function(form,data,hasError) { if (!hasError) doImportSubmit(0); return false; }',
	),
)); /* @var TbActiveForm $form */
?>
<fieldset>
	<legend>
		<?php
		// Button group
		$this->widget('bootstrap.widgets.TbButtonGroup', array(
			'type' => 'normal',
			'toggle' => 'radio',
			'buttons' => array(
				array('label' => '导入 CSV 文件', 'url' => array($this->route, 'type' => 'csv')),
				array('label' => '从数据库导入', 'url' => array($this->route, 'type' => 'db'), 'disabled' => true, 'active' => true),
			),
		));
		?>
	</legend>
	<?php
	// inputs
	echo $form->hiddenField($model, 'offset');
	echo $form->dropDownListRow($model, 'driver', $model->dbDrivers());
	echo $form->textFieldRow($model, 'hostname', array('class' => 'no-sqlite'));
	echo $form->textFieldRow($model, 'dbuser', array('class' => 'no-sqlite'));
	echo $form->textFieldRow($model, 'dbpass', array('class' => 'no-sqlite'));
	echo $form->textFieldRow($model, 'dbname', array('hint' => '对于 SQLite，在此填写服务端上的数据库路径'));
	echo $form->textAreaRow($model, 'queryString', array('class' => 'span10', 'rows' => 2));
	?>
	<div class="control-group">
		<div class="controls">
			<?php
			$this->widget('bootstrap.widgets.TbButton', array(
				'id' => 'btn-submit',
				'buttonType' => 'submit',
				'type' => 'primary',
				'label' => '开始导入',
			));
			?>
		</div>
	</div>
</fieldset>
<?php $this->endWidget(); ?>
<?php $this->endWidget(); ?>
<?php
$this->beginWidget('bootstrap.widgets.TbModal', array(
	'id' => 'modal-import',
	'options' => array('backdrop' => 'static'),
));
?>
<div class="modal-header">
	<h4>数据导入中，请耐心等待……</h4>
</div>
<div class="modal-body">
	<p></p>
</div>
<div class="modal-footer">
	<?php
	$this->widget('bootstrap.widgets.TbButton', array(
		'type' => 'primary',
		'label' => '完成',
		'htmlOptions' => array('id' => 'btn-finish', 'style' => 'display:none;'),
	));
	?>
</div>
<?php $this->endWidget(); ?>
<script type="text/javascript" pos="ready">
	// submit callback
	var total, fail, step;
	function doImportSubmit(offset)
	{
		var $form = $('#form-import');
		if (offset === 0) {
			$('#modal-import h4').html('数据导入中……');
			$('#btn-finish').hide();
			total = fail = 0;
			step = 1;
		}
		$('#modal-import p').html('正在处理第 ' + step + ' 批，已处理 ' + total + ' 条，失败 ' + fail + ' 条');
		$('#modal-import').modal({
			keyboard: false,
			show: true,
			backdrop: false
		});
		$('#<?php echo CHtml::ActiveId($model, 'offset'); ?>').val(offset);
		$.ajax({
			url: $form.attr('action'),
			data: $form.serialize(),
			dataType: 'json',
			type: 'post',
			success: function(data, status) {
				if (data.ok === 0)
				{
					$('#modal-import h4').html('导入出错');
					$('#modal-import p').html(data.msg);
					$('#btn-finish').show();
				}
				else
				{
					var offset = data.offset;
					fail += data.fail;
					total += data.count;
					step++;
					if (offset < 0)
					{
						$('#modal-import h4').html('导入完成');
						$('#modal-import p').html('处理完成，共处理 ' + total + ' 条数据，失败 ' + fail + ' 条');
						$('#btn-finish').show();
					}
					else
					{
						setTimeout(function() {
							doImportSubmit(offset);
						}, 200);
					}
				}
			},
			error: function(req, status, err) {
				alert(err);
				$('#btn-finish').show();
			}
		});
	}
	// binds
	$('#btn-finish').click(function() {
		$('#modal-import').modal('hide');
	});
	$('#<?php echo CHtml::activeId($model, 'driver'); ?>').change(function() {
		if ($(this).val() === 'sqlite' || $(this).val() === 'sqlite3')
		{
			$('#form-import input.no-sqlite').each(function() {
				$(this).closest('.control-group').hide();
			});
		}
		else
		{
			$('#form-import input.no-sqlite').each(function() {
				$(this).closest('.control-group').show();
			});
		}
	});
</script>
